package b2b

import (
	"fmt"
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/config"

	"github.com/gin-gonic/gin"
)

// 列表查询
func StaticConfig(c *gin.Context) {

	requestData := struct {
		AccessKey      string `json:"access_key"`
		AccessPassword string `json:"access_password"`
		Money          int    `json:"money"`
		MuchId         string `json:"much_id"`
		PaymentMethod  int    `json:"payment_method"`
		PushBaseUrl    string `json:"push_base_url"`
		Status         int    `json:"status"`
		Page           int    `json:"page"`
		Limit          int    `json:"limit"`
	}{}

	c.ShouldBindJSON(&requestData)
	where := map[string]interface{}{}

	if requestData.PushBaseUrl != "" {
		where["push_base_url like ?"] = "%%" + requestData.PushBaseUrl + "%%"
	}

	if requestData.AccessKey != "" {
		where["access_key like ?"] = "%%" + requestData.AccessKey + "%%"
	}

	if requestData.AccessPassword != "" {
		where["access_password like ?"] = "%%" + requestData.AccessPassword + "%%"
	}

	if requestData.MuchId != "" {
		where["much_id like ?"] = "%%" + requestData.MuchId + "%%"
	}

	if requestData.Status != -1 {
		where["status"] = requestData.Status
	}

	if requestData.PaymentMethod != -1 {
		where["payment_method"] = requestData.PaymentMethod
	}

	//查询信息
	var configStaticParkingStruct config.ConfigStaticParkingStruct
	var configStaticParkingData []config.ConfigStaticParkingStruct
	model.GetModelData(
		model.Db,
		configStaticParkingStruct.TableName(),
		&configStaticParkingData,
		model.QueryOptions{
			Page:       requestData.Page,
			PageSize:   requestData.Limit,
			Conditions: where,
			Debug:      true,
		},
	)

	//数据格式化
	returnData := make([]map[string]interface{}, 0)
	for _, v := range configStaticParkingData {
		returnData = append(returnData, map[string]interface{}{
			"id":              v.Id,
			"alias":           v.Alias,
			"push_base_url":   v.PushBaseUrl,
			"access_key":      v.AccessKey,
			"access_password": v.AccessPassword,
			"much_id":         v.MuchId,
			"status":          v.Status,
			"money":           v.Money,
			"payment_method":  v.PaymentMethod,
		})
	}

	//总数查询
	count, _ := model.CountModelData(
		model.Db,
		configStaticParkingStruct.TableName(),
		model.QueryOptions{
			Conditions: where,
		},
	)

	controller.Success(c,
		map[string]interface{}{
			"list":        returnData,
			"total":       count,
			"pageSize":    requestData.Limit,
			"currentPage": requestData.Page,
		})
}

// 数据更新
func StaticConfigUpdate(c *gin.Context) {
	updateData := struct {
		Id             int    `json:"id"`
		AccessKey      string `json:"access_key"`
		AccessPassword string `json:"access_password"`
		Money          int    `json:"money"`
		MuchId         string `json:"much_id"`
		PaymentMethod  int    `json:"payment_method"`
		PushBaseUrl    string `json:"push_base_url"`
		Status         int    `json:"status"`
	}{}

	c.ShouldBindJSON(&updateData)

	var configStaticParkingStruct config.ConfigStaticParkingStruct

	if err := model.UpdateModelData(
		model.Db,
		configStaticParkingStruct.TableName(),
		map[string]interface{}{
			"id": updateData.Id,
		},
		map[string]interface{}{
			"push_base_url":   updateData.PushBaseUrl,
			"access_key":      updateData.AccessKey,
			"access_password": updateData.AccessPassword,
			"much_id":         updateData.MuchId,
			"status":          updateData.Status,
			"money":           updateData.Money,
			"payment_method":  updateData.PaymentMethod,
		},
	); err != nil {
		controller.Fail(c, fmt.Sprintf("修改失败:%s", err.Error()))
		return
	}

	controller.Success(c, "")
}
